{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> Coin Flip: A Probabilistic Bit </h2>\n",
    "\n",
    "<h3> A fair coin </h3>\n",
    "\n",
    "A coin has two sides: <i>Head</i> and <i>Tail</i>.\n",
    "\n",
    "After flipping a coin, we can get a Head or Tail. We can represent these two cases by a single bit:\n",
    "<ul>\n",
    "    <li> 0 represents Head </li>\n",
    "    <li> 1 represents Tail </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Flipping a fair coin </h3>\n",
    "\n",
    "If our coin is fair, then the probability of obtaining a Head or Tail is equal:\n",
    "\n",
    "$ p= \\dfrac{1}{2} = 0.5 $.\n",
    "\n",
    "Coin-flipping can be defined as an operator:\n",
    "<ul>\n",
    "    <li> $ FairCoin(Head) = \\frac{1}{2} Head + \\frac{1}{2}Tail $ </li>\n",
    "    <li> $ FairCoin(Tail) = \\frac{1}{2} Head + \\frac{1}{2}Tail $ </li>\n",
    "</ul>\n",
    "$\n",
    "FairCoin = \\begin{array}{c|cc} & \\mathbf{Head} & \\mathbf{Tail} \\\\ \\hline \\mathbf{Head} & \\dfrac{1}{2} & \\dfrac{1}{2} \\\\  \\mathbf{Tail} & \\dfrac{1}{2} & \\dfrac{1}{2}  \\end{array}\n",
    "$\n",
    "\n",
    "Or, by using 0 and 1:\n",
    "\n",
    "$\n",
    "FairCoin = \\begin{array}{c|cc} & \\mathbf{0} & \\mathbf{1} \\\\ \\hline \\mathbf{0} & \\dfrac{1}{2} & \\dfrac{1}{2} \\\\  \\mathbf{1} & \\dfrac{1}{2} & \\dfrac{1}{2}  \\end{array}\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1: Simulating FairCoin in Python</h3>\n",
    "\n",
    "Flip a fair coin 100 times. Calcuate the total number of heads and tails, and then compare them.\n",
    "\n",
    "Do the same experiment 1000 times.\n",
    "\n",
    "Do the same experiment 10,000 times.\n",
    "\n",
    "Do the same experiment 100,000 times.\n",
    "\n",
    "Do your results get close to the ideal case (the numbers of heads and tails are the same)?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first we import a procedure for picking a random number\n",
    "from random import randrange\n",
    "# randrange(m) returns a number randomly from the list {0,1,...,m-1}\n",
    "# randrange(10) returns a number randomly from the list {0,1,...,9}\n",
    "# here is an example\n",
    "r=randrange(5)\n",
    "print(\"I picked a random number between 0 and 4, which is \",r)\n",
    "\n",
    "#\n",
    "# your solution is here\n",
    "#\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B26_Coin_Flip_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Flipping a biased coin </h3>\n",
    "\n",
    "Our coin may have a bias. \n",
    "\n",
    "For example, the probability of getting head is greater than the probability of getting tail.\n",
    "\n",
    "Here is an example:\n",
    "\n",
    "$\n",
    "BiasedCoin = \\begin{array}{c|cc} & \\mathbf{Head} & \\mathbf{Tail} \\\\ \\hline \\mathbf{Head} & 0.6 & 0.6 \\\\  \\mathbf{Tail} & 0.4 & 0.4  \\end{array}\n",
    "$\n",
    "\n",
    "Or, by using 0 and 1 as the states:\n",
    "\n",
    "$\n",
    "BiasedCoin = \\begin{array}{c|cc} & \\mathbf{0} & \\mathbf{1} \\\\ \\hline \\mathbf{0} & 0.6 & 0.6\\\\  \\mathbf{1} & 0.4 & 0.4 \\end{array}\n",
    "$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2: Simulating BiasedCoin in Python</h3>\n",
    "\n",
    "Flip the following biased coin 100 times. Calcuate the total number of heads and tails, and then compare them.\n",
    "\n",
    "$\n",
    "BiasedCoin = \\begin{array}{c|cc} & \\mathbf{Head} & \\mathbf{Tail} \\\\ \\hline \\mathbf{Head} & 0.6 & 0.6 \\\\  \\mathbf{Tail} & 0.4 & 0.4  \\end{array}\n",
    "$\n",
    "\n",
    "\n",
    "Do the same experiment 1000 times.\n",
    "\n",
    "Do the same experiment 10,000 times.\n",
    "\n",
    "Do the same experiment 100,000 times.\n",
    "\n",
    "Do your results get close to the ideal case $ \\mypar{ \\dfrac{ \\mbox{# of heads} }{ \\mbox{# of tails} } = \\dfrac{0.6}{0.4} = 1.50000000 } $?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B26_Coin_Flip_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first we import a procedure for picking a random number\n",
    "from random import randrange\n",
    "#\n",
    "# your solution is here\n",
    "#\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
